package com.focus.pay.wx.action.v3;

import cn.hutool.http.Method;
import com.focus.pay.wx.config.WxPayConfig;
import com.focus.pay.wx.constant.WxPayConstant;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.enums.ChannelTypeEnum;
import com.focus.pay.core.enums.Version;
import com.focus.pay.core.exception.PayException;
import com.focus.pay.core.pay.PayOrderInterface;
import com.focus.pay.core.utils.HttpClientUtils;
import com.focus.pay.wx.model.order.v3.WxPayModelUnified;
import com.focus.pay.wx.model.order.v3.WxPayModelUnifiedResponse;
import com.focus.pay.wx.utils.WxPayUtils;

/**
 * @Author: 赵参谋
 * @Date: 2021/10/11 17:47
 * @Description: 微信APP支付V3
 * <link>https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml</link>
 */
@PayOrderHandler(channelType = ChannelTypeEnum.WxType.App,version = Version.VERSION_3)
public class WxPayOrderApp extends WxPay implements PayOrderInterface<WxPayModelUnifiedResponse, WxPayModelUnified, WxPayConfig> {


    @Override
    public WxPayModelUnifiedResponse payOrder(WxPayModelUnified wxPayModelUnified, WxPayConfig wxPayConfig) throws PayException {
        DeployParams deployParams = super.instanceDeployParams(WxPayConstant.WxPayPathV3.PAY_ORDER_UNIFIED,new String[]{ChannelTypeEnum.WxType.App});
        String res = HttpClientUtils.doPost(
                super.buildRequestPath(deployParams),
                super.buildBody(wxPayModelUnified,wxPayConfig),
                super.buildHead(Method.POST,wxPayModelUnified,wxPayConfig,deployParams));
        return this.handle(res,wxPayConfig);
    }

    /**
     * 返回数据处理
     * @param res
     * @param wxPayConfig
     * @return
     * @throws Exception
     */
    private WxPayModelUnifiedResponse handle(String res, WxPayConfig wxPayConfig) throws PayException {
        WxPayModelUnifiedResponse wxPayModelUnifiedResponse = super.resolvingResult(res, WxPayModelUnifiedResponse.class);
        wxPayModelUnifiedResponse.setPageData(WxPayUtils.AppRecall(wxPayModelUnifiedResponse,wxPayConfig, Version.VERSION_3));
        return wxPayModelUnifiedResponse;
    }
}
